/* 
 * File:   main.c
 * Author: bogdan
 *
 * Created on December 9, 2012, 8:05 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <p32xxxx.h>
#include <plib.h>

#include "cfgbits.h"
#include "flash.h"

/*
 * 
 */
void main(void)
{
    unsigned char   i   =   0; //general counter
    unsigned int    start_address = 0x000100;
    unsigned int    address = 0x000100;
    unsigned char   data = 0x00;
    unsigned char   pack_size = 8; //defines the data pack size to be transfered

    unsigned char   buffer[pack_size];
    unsigned char   out_buf[pack_size];

    buffer[0]=2;
    buffer[1]=4;
    buffer[2]=5;
    buffer[3]=0;
    buffer[4]=9;
    buffer[5]=2;
    buffer[6]=7;
    buffer[7]=5;

    ConfigFlash();

    data = ReadFlashStatus();
    
    address = ReadFlashJedecID();

    UnprotectFlash();

    while (ReadFlashStatus() != 0x80);

    EraseFlash();

    address = start_address;
    data =0x10;

    //TODO v2 use the memory AAI


    WriteFlash(address, pack_size, buffer);
/*
    while(i != pack_size)
    {
        WriteFlash(address,pack_size,buffer[i]);
        address++;
        i++;
        delay_ms(20);
    }
*/
    ReadFlashHS(start_address, pack_size, out_buf);

}